<template>
  <div class="comment-item" :style="{ marginLeft: depth * 30 + 'px' }">
    <div class="comment-header">
      <img :src="comment.userAvatar" class="avatar" />
      <span class="username">{{ comment.userName }}</span>
      <span class="time">{{ comment.time }}</span>
    </div>
    <div class="comment-content">
      {{ comment.content }}
    </div>
    <div class="comment-actions">
      <el-button type="text" @click="handleReply">回复</el-button>
    </div>

    <!-- 递归渲染子评论 -->
    <CommentItem
        v-for="child in comment.children"
        :key="child.id"
        :comment="child"
        :depth="depth + 1"
        @reply="handleChildReply"
    />
  </div>
</template>

<script setup>
import CommentItem from './CommentItem.vue'
import {reactive} from "vue";
import comment from "@/views/Comment.vue";

const data = reactive({

})

defineProps({
  comment: {
    type: Object,
    required: true
  },
  depth: {
    type: Number,
    default: 0
  }
})

const emit = defineEmits(['reply'])

const handleReply = () => {
  emit('reply', comment.id)
}

const handleChildReply = (parentId) => {
  emit('reply', parentId)
}
</script>

<style scoped>
.comment-item {
  margin: 15px 0;
  padding: 15px;
  border: 1px solid #eee;
  border-radius: 4px;
  transition: all 0.3s;
}
.comment-item:hover {
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}
.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 10px;
}
.username {
  font-weight: bold;
  margin-right: 10px;
}
.time {
  color: #999;
  font-size: 12px;
}
.comment-content {
  margin: 10px 0;
  line-height: 1.6;
}
.comment-actions {
  margin-top: 10px;
}
</style>